package tps12.axwax.client.view;

import java.util.Collection;

import tps12.axwax.client.presenter.SolvePuzzlePresenter;
import tps12.axwax.client.presenter.SolvePuzzlePresenter.GridDisplay;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class SolvePuzzle extends Composite
	implements SolvePuzzlePresenter.Display {
	interface MyUiBinder extends UiBinder<FlowPanel, SolvePuzzle> {}
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	public SolvePuzzle() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	@UiField FlowPanel head;
	@UiField FocusPanel focus;
	@UiField Label title;
	@UiField HTML instructions;
	@UiField FlowPanel top;
	@UiField FlowPanel clues;
	
	@UiField SpanElement email;
	@UiField SpanElement status;
	@UiField Anchor login;
	@UiField Anchor logout;

	public Widget asWidget() { return this; }

	@Override
	public void addClues(String label, Collection<String> clues) {
		ClueList list = new ClueList(label, clues);
		list.addOpenHandler(new OpenHandler<Clue>() {
			@Override
			public void onOpen(OpenEvent<Clue> event) {
				collapseClues(event.getTarget());
			}
		});
		this.clues.add(list);
		int n = this.clues.getWidgetCount();
		for (int i = 0; i < n; i++) {
			this.clues.getWidget(i).setWidth((1/(float)n) * 100 + "%");
			this.clues.getWidget(i).getElement().getStyle()
				.setLeft(((double)i/(double)n) * 100, Unit.PCT);
		}
	}
	
	@Override
	public void setHeader(String title, String instructions) {
		this.title.setText(title);
		this.instructions.setHTML(instructions);
	}

	@Override
	public GridDisplay createGrid(int width, int height) {
		PuzzleGrid grid = new PuzzleGrid(new Grid(height, width));
		focus.add(grid);
		String w = grid.getWidth();
		focus.getElement().getStyle().setProperty("width", w);
		head.getElement().getStyle().setProperty("marginRight", w);
		top.getElement().getStyle().setProperty("height", grid.getHeight());
		return grid;
	}
	
	void collapseClues(Clue open) {
		for (int i = 0; i < clues.getWidgetCount(); i++)
			((ClueList)clues.getWidget(i)).closeExcept(open);
	}
	
	@UiHandler("focus")
	public void handleFocus(FocusEvent event) {
		collapseClues(null);
	}

	@Override
	public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
		return focus.addKeyPressHandler(handler);
	}
	
	@Override
	public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
		return focus.addKeyDownHandler(handler);
	}
	
	@Override
	public void setLoginUrl(String url) {
		logout.setVisible(false);
		login.setHref(url);
		login.setVisible(true);
	}
	
	@Override
	public void setLogoutUrl(String url) {
		login.setVisible(false);
		logout.setHref(url);
		logout.setVisible(true);
	}
	
	@Override
	public void setUser(String name) {
		email.setInnerText(name + " |");
	}

	@Override
	public void setCommStatus(boolean good) {
		status.setInnerText(good ? "Good |" : "Bad |");
	}
}